package com.lovo.dao.impl;

import com.lovo.bean.UserBean;
import com.lovo.dao.BaseDao;
import com.lovo.dao.IUserDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String username, String password) {
        String sql = "select * from user_info where username=? and password=?";

        try {
            setConnection();
            ps = con.prepareStatement(sql);
            ps.setObject(1, username);
            ps.setObject(2, password);
            rs = ps.executeQuery();
            if (rs.next()) {
                UserBean userBean = new UserBean();
                userBean.setId(rs.getInt("user_id"));
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                return userBean;
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return null;

    }

    @Override
    public void updatePwd(int userId, String password) {

        try {
            this.setConnection();
            ps = con.prepareStatement("update user_info set password=? where user_id=?");
            ps.setObject(1, password);
            ps.setObject(2, userId);
            ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }

    }

    public static void main(String[] args) {
        UserDaoImpl userDao = new UserDaoImpl();
//        System.out.println(userDao.login("aaaaaa","123123"));
        userDao.updatePwd(1,"888888");
    }
}
